package smartterracedfieldsolution.smartteracedfieldsolution.common.result;

/**
 * 统一返回包装类
 * 统一后端返回的数据类型
 */

import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
public class Result <T>{
    private Integer code;
    private String message;
    private T data;

    public Result(){}

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 静态方法，用于快速创建返回对象
    public static <T> Result<T> success(T data) {
        return new Result<>(HttpStatus.OK.value(), "操作成功", data);
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<>(HttpStatus.OK.value(), message, data);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<>(code, message, null);
    }

    public static <T> Result<T> fail(Integer code, String message, T data) {
        return new Result<>(code, message, data);
    }

    @Override
    public String toString() {
        return "Result{" +"code=" + code + ", message='" + message + '\'' + ", data=" + data + '}';
    }
}